home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / graphics / gif2rpc / source / 8bpp / c / sierra2 < prev    next >
Encoding:
Text File  |  1995-10-16  |  4.5 KB  |  146 lines

  1. /* sierra2.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  *
  5.  * filter:              *  4  3
  6.  *                1  2  3  2  1         (1/16)
  7.  */
  8.  
  9. #include "internal.h"
  10.  
  11. #include <assert.h>
  12. #include <string.h>             /* memset() */
  13. #include <stdlib.h>             /* calloc() */
  14.  
  15. #include "OS:hourglass.h"
  16. #include "OS:macros.h"
  17.  
  18.  
  19.  
  20. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21.  */
  22.  
  23. extern bool process_gif_8bpp_sierra2(
  24.                 const process_gif       *p) {
  25.   byte                  *rove;
  26.   int                   width, height;
  27.   int                   x, y;
  28.   const os_colour       *palette;
  29.   int                   line_length;
  30.   int                   *buffer, *this_row, *next_row;
  31.   int                   buffer_width;
  32.   int                   red, grn, blu;
  33.   os_colour             colour;
  34.   int                   error_red, error_grn, error_blu;
  35.   int                   temp_red, temp_grn, temp_blu;
  36.   rgbtupleout           error;
  37.   rgbtupleout_fn        fn;
  38.  
  39.   assert(p);
  40.   assert(p->pixel_width > 0);
  41.   assert(p->pixel_height > 0);
  42.   assert(p->in_palette.colours);
  43.   assert(p->fn);
  44.  
  45.   /*
  46.    * sierra2 requires storing error info for two pixels to right and two pixels to left
  47.    * so we will just allocate two extra columns for each row and not do any edge checks
  48.    * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
  49.    */
  50.   buffer_width = (2 + p->pixel_width + 2) * 3;
  51.   buffer = calloc(1, sizeof(*buffer) * (buffer_width * 2));
  52.   if (!buffer) {
  53.     /*
  54.      * oh dear
  55.      */
  56.     return TRUE;
  57.   }
  58.  
  59.   error.palette = p->out_palette;               /* structure copy */
  60.   /*
  61.    * not we pre-load values from the process_gif array
  62.    * because it considerably helps the compiler produce better code
  63.    */
  64.   rove = p->buffer;
  65.   width = p->pixel_width;
  66.   height = p->pixel_height;
  67.   palette = p->in_palette.colours;
  68.   line_length = p->line_length;
  69.   fn = p->fn;
  70.   for (y= height; (y > 0); y--) {
  71.     if ((y & 7) == 0) {
  72.       xhourglass_percentage((y * 100) / height);
  73.     }
  74.     this_row = buffer + (buffer_width * ((y + 2) % 2)) + 2*3;
  75.     next_row = buffer + (buffer_width * ((y + 1) % 2)) + 2*3;
  76.     /* bottom row has no errors */
  77.     memset(next_row, 0, sizeof(*next_row) * (buffer_width - 2*3));
  78.     /*
  79.      * note that just because we are actually scanning/outputting right to left
  80.      * doesn't matter as far as sierra2 is concerned
  81.      * although it might help if we could ``snake''
  82.      */
  83.     for (x= width - 1; (x >= 0); x--) {
  84.       INPUT;
  85.  
  86.       red += *this_row++;                               /* add in errors from all rows */
  87.       grn += *this_row++;
  88.       blu += *this_row++;
  89.  
  90.       PROCESS;
  91.  
  92.       temp_red = (red * 4) / 32; error_red = red - (1 * temp_red);
  93.       temp_grn = (grn * 4) / 32; error_grn = grn - (1 * temp_grn);
  94.       temp_blu = (blu * 4) / 32; error_blu = blu - (1 * temp_blu);
  95.  
  96.       this_row[ 0] += temp_red;                         /* this[ 1] += 4/16 */
  97.       this_row[ 1] += temp_grn;
  98.       this_row[ 2] += temp_blu;
  99.  
  100.       temp_red >>= 1; error_red -= 2 * temp_red;
  101.       temp_grn >>= 1; error_grn -= 2 * temp_grn;
  102.       temp_blu >>= 1; error_blu -= 2 * temp_blu;
  103.  
  104.       next_row[ 3] += temp_red;                         /* next[ 1] += 2/16 */
  105.       next_row[ 4] += temp_grn;
  106.       next_row[ 5] += temp_blu;
  107.  
  108.       next_row[-3] += temp_red;                         /* next[-1] += 2/16 */
  109.       next_row[-2] += temp_grn;
  110.       next_row[-1] += temp_blu;
  111.  
  112.       temp_red += temp_red >> 1; error_red -= 2 * temp_red;
  113.       temp_grn += temp_grn >> 1; error_grn -= 2 * temp_grn;
  114.       temp_blu += temp_blu >> 1; error_blu -= 2 * temp_blu;
  115.  
  116.       this_row[ 3] += temp_red;                         /* this[ 2] += 3/16 */
  117.       this_row[ 4] += temp_grn;
  118.       this_row[ 5] += temp_blu;
  119.  
  120.       next_row[ 0] += temp_red;                         /* next[ 0] += 3/16 */
  121.       next_row[ 1] += temp_grn;
  122.       next_row[ 2] += temp_blu;
  123.  
  124.       temp_red = red >> 4; error_red -= temp_red;
  125.       temp_grn = grn >> 4; error_grn -= temp_grn;
  126.       temp_blu = blu >> 4; error_blu -= temp_blu;
  127.  
  128.       next_row[-6] += temp_red;                         /* next[-2] += 1/16 */
  129.       next_row[-5] += temp_grn;
  130.       next_row[-4] += temp_blu;
  131.  
  132.       next_row[ 6] += error_red;                        /* next[ 2] += 1/16 */
  133.       next_row[ 7] += error_grn;
  134.       next_row[ 8] += error_blu;
  135.  
  136.       next_row += 3;                                    /* adjust next_row pointer */
  137.  
  138.     }
  139.     rove += line_length;
  140.   }
  141.   free(buffer);
  142.   return FALSE;
  143. }
  144.  
  145.  
  146.